﻿Imports Excel = Microsoft.Office.Interop.Excel
Public Class frmDocFileExcel

    Public xlApp As Excel.Application
    Public xlWorkBook As Excel.Workbook
    Public xlWorkSheet As Excel.Worksheet
    Public duongdan As String
    Public tensheet As String

    Private Sub btnChonLuuFileBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChonLuuFileBackup.Click
        'txtFileExcel.Text = ""
        'cmbSheet.Text = ""
        Dim diag As New OpenFileDialog
        diag.FileName = "Excel (*.xls;*.xlsx)|*.xls;*.xlsx"
        With diag
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                txtFileExcel.Text = .FileName
            End If
        End With

        Try
            'Mở Excel Application
            xlApp = New Excel.Application

            'Mở File Excel đã chọn
            xlWorkBook = xlApp.Workbooks.Open(txtFileExcel.Text)

            'Lấy tên tất cả các Sheet
            For Each Me.xlWorkSheet In xlWorkBook.Worksheets
                cmbSheet.Items.Add(xlWorkSheet.Name)
            Next xlWorkSheet

            'Gán tên Sheet đầu tiên cho Combobox
            cmbSheet.Text = (cmbSheet.Items(0).ToString())
            xlWorkBook.Close()
            xlApp.Quit()

        Catch ex As Exception
            MessageBox.Show("Bạn chọn file Excel cần đọc dữ liệu!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Hand)
        End Try
        
    End Sub

    Private Sub btnBiSaoLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBiSaoLuu.Click
        duongdan = ""
        tensheet = ""
        Me.Close()
    End Sub

    Private Sub btnSaoLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaoLuu.Click
        duongdan = txtFileExcel.Text
        tensheet = cmbSheet.Text
        Me.Close()
    End Sub
    'Private Sub frmDocFileExcel_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
    '    duongdan = ""
    '    tensheet = ""
    'End Sub

    Private Sub frmDocFileExcel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtFileExcel.Text = ""
        'cmbSheet.DataBindings = ""
    End Sub
End Class